<?php
/**
 * Created by PhpStorm.
 * User: thanatos
 * Date: 2018-11-30
 * Time: 13:49
 */

namespace App\Admin\Controllers;


use App\Http\Controllers\Controller;
use App\Models\Services;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ServicesController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('套餐管理')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('套餐')
            ->description('详细')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('套餐')
            ->description('修改')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('套餐')
            ->description()
            ->body($this->form());
    }


    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Services::findOrFail($id));

        $show->id('ID');
        $show->fields([
            'name' => '套餐名',
            'times' => '时长',
            'total_price' => '总价格',
            'month_price' => '每月价格',
            'top_tags' => '上标签',
            'bottom_tags' => '下标签',
        ]);
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Services());

        $grid->id('ID')->sortable();
        $grid->columns([
            'name' => '套餐名',
            'times' => '时长',
            'total_price' => '总价格',
            'month_price' => '每月价格',
            'top_tags' => '上标签',
            'bottom_tags' => '下标签',
        ]);
        $grid->created_at('创建时间');

        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Services());

        $form->display('id', 'ID');
        $form->text('name', '套餐名')->required()->placeholder('请输入套餐名');
        $form->text('source', '来源')->placeholder('请输入来源')->required()->help('产品应该用App来源');
        $form->currency('total_price', '价格')->required();
        $form->currency('month_price', '月平均价格')->required();
        $form->select('times', '时长')
            ->options([
            1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12,
        ])->required()
            ->help('连续包月时长');
        $form->text('free_times', '免费时长')->placeholder('请输入免费时长（天）')->default(0);
        $form->select('is_recommend', '是否推荐')->options([ 1 => '是', 0 => '否' ]);
        $form->text('top_tags', '上标签')->placeholder('请输入上标签')->help('产品显示是上边显示的文字')->default('');
        $form->text('bottom_tags', '下标签')->placeholder('请输入上标签')->help('产品显示是下边显示的文字')->default('');
        $form->display('created_at', '创建时间');
        $form->display('updated_at', '修改时间');

        return $form;
    }

}